package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private DishMapper dishMapper;


    @Override
    public void addShopCart(ShoppingCartDTO shoppingCartDTO) {

        //判断传入的购物车数据是否已经存在于表中 存在则数量+1

        ShoppingCart cart = new ShoppingCart();
        //设置用户id
        cart.setUserId(BaseContext.getCurrentId());
        //复制 属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO, cart);

        //查询购物车数据表 看是否存在要添加得到数据
        List<ShoppingCart> list = shopCartMapper.getShopCart(cart);

        if (list.size() > 0 && list != null) {
            //存在数据 数量+1
            cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shopCartMapper.update(cart);
        } else {
            //不存在 插入数据
            cart.setCreateTime(LocalDateTime.now());

            //判断添加到购物车的是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                //添加的是菜品
                Dish dish = shopCartMapper.getdish(shoppingCartDTO.getDishId());
                cart.setNumber(1);//设置数量
                cart.setImage(dish.getImage());//设置图片路径
                cart.setAmount(dish.getPrice());//设置价格
                cart.setCreateTime(LocalDateTime.now());//设置创建时间
                cart.setName(dish.getName());//菜品名称
            } else {
                //添加的是套餐
                Setmeal setmeal = shopCartMapper.getSetmeal(shoppingCartDTO.getSetmealId());
                cart.setCreateTime(LocalDateTime.now());
                cart.setImage(setmeal.getImage());
                cart.setNumber(1);
                cart.setAmount(setmeal.getPrice());
                cart.setName(setmeal.getName());
            }
            shopCartMapper.insert(cart);

        }
    }

    @Override
    public List<ShoppingCart> getShopCart() {
        List<ShoppingCart> list = shopCartMapper.getShopCartByuserId(BaseContext.getCurrentId());

        return  list ;
    }

    @Override
    public void deleteShoppingCart(Long userId) {

        shopCartMapper.deleteShoppingCart(userId);

    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {

        if (shoppingCartDTO.getDishId() != null){
            //删减菜品 判断菜品数量

            Integer numberByid = shopCartMapper.getNumberByid(shoppingCartDTO.getDishId());

            if (numberByid > 1){
                //数量大于1 则数量-1
                ShoppingCart cart = new ShoppingCart();
                cart.setUserId(BaseContext.getCurrentId());
                cart.setDishId(shoppingCartDTO.getDishId());
                cart.setNumber(numberByid - 1);
                shopCartMapper.update(cart);
            }else {
                //数量等于1 则删除该数据
                shopCartMapper.deleteDish(shoppingCartDTO.getDishId());
            }
        }

        else {
            //删减套餐
            Integer numberByid = shopCartMapper.getNumberByid(shoppingCartDTO.getSetmealId());


            shopCartMapper.deleteDish(shoppingCartDTO.getSetmealId());
            if (numberByid > 1){
                //数量大于1 则数量-1
                ShoppingCart cart = new ShoppingCart();
                cart.setUserId(BaseContext.getCurrentId());
                cart.setSetmealId(shoppingCartDTO.getSetmealId());
                cart.setNumber(numberByid - 1);
                shopCartMapper.update(cart);
            }else {
                //数量等于1 则删除该数据
                shopCartMapper.deleteDish(shoppingCartDTO.getSetmealId());
            }
        }


    }
}
